<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Hash;
use Log;
use App\Models\ThermalLog;
use App\Models\DeviceLog;
use App\Models\Device;
//20210603
class ThermalController extends Controller
{
    //接受数据
    public function ThermalData(Request $request){
        $info = $request->all();
        // Log::info($info);
        $data['max'] = $info['maxTemperature'];
        $data['min'] = $info['minTemperature'];
        $data['average'] = $info['averageTemperature'];
        $data['pushtime'] = $info['time'];
        $data['created_at'] = date('Y-m-d H:i:s');
        $data['deviceId'] = $info['ip'];
        ThermalLog::insert($data);
        return $info;
    }
    //接受报警
    public function ThermalWarning(Request $request){
        $info = $request->all();
        $info['time'] = time();
        $hot = Cache::get('hotlog');
        if(isset($hot)){
            if((time()-$hot['time'])>60||$info['ip']!=$hot['ip']){
                switch ($info['warning']) {
                    case 1:
                        $data['msg'] = '遮挡报警';
                        break;
                    case 2:
                        $data['msg'] = '温度报警';
                        break;
                    case 3:
                        $data['msg'] = '移动报警';
                        break;
                }
                $data['pushtime'] = date('Y-m-d H:i:s',$info['time']);
                $data['created_at'] = date('Y-m-d H:i:s');
                $data['type'] = 1;
                $data['deviceId'] = Device::where(['gatewayId'=>$info['ip']])->select('deviceId');
                $data['device_type'] = 9;
                Log::info($data);
                DeviceLog::insert($data);
                Cache::put('hotlog',$info);
            }
        }else{
            Cache::put('hotlog',$info);
        }
        return $info;
    }
}